package com.schoolcloud.share.controller;

import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.R;
import com.schoolcloud.share.entity.Categories;
import com.schoolcloud.share.query.CategoriesQuery;
import com.schoolcloud.share.service.CategoriesService;
import com.schoolcloud.share.vo.CategoryTreeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/share/category")
@RequiredArgsConstructor
public class ShareCategoryController {
    private final CategoriesService categoryService;

    @GetMapping("/page")
    public R<PageDTO<Categories>> getCategoryPage(CategoriesQuery query) {
        return R.ok(categoryService.getCategoryPage(query));
    }
    @GetMapping("/parents/tree")
    public R<List<CategoryTreeVO>> getParentsTree(){
        List<CategoryTreeVO> parentsTree = categoryService.getParentsTree();
        return R.ok(parentsTree);
    }
    @GetMapping("/tree")
    public R<List<CategoryTreeVO>> getTree(){
        List<CategoryTreeVO> tree = categoryService.getTreeList();
        return R.ok(tree);
    }
    @PostMapping
    public R<?> save(@RequestBody Categories category){
        category.setCreatedAt(LocalDateTime.now());
        categoryService.save(category);
        return R.ok().setMsg("新增成功！");
    }
    @PutMapping
    public R<?> update(@RequestBody Categories category){
        category.setUpdatedAt(LocalDateTime.now());
        categoryService.updateById(category);
        return R.ok().setMsg("更新成功!");
    }
    @GetMapping("/{id}")
    public R<Categories> get(@PathVariable Integer id){
        Categories category = categoryService.getById(id);
        return R.ok(category);
    }
}
